Geolocation in iOS by Alasdair Allan
Author:Alasdair Allan [Alasdair Allan]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Data Transmission Systems / Wireless
ISBN: 9781449308438
Publisher: O'Reilly Media
Published: 2012-10-02T04:00:00+00:00
Annotating Maps
Adding simple map annotations using the Map Kit framework is actually pretty easy. The first thing you need to do is create a class that implements the MKAnnotation delegate protocol. Right-click the Location group in the Project Navigator panel and New File to create a new Objective-C class (make it an NSObject subclass). Name the new class SimpleAnnotation when prompted.
Open the SimpleAnnotation.h interface file Xcode just created in the Standard editor and modify it as follows:
#import <Foundation/Foundation.h> @interface SimpleAnnotation : NSObject <MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *subtitle; + (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord; - (id)initWithCoordinate:(CLLocationCoordinate2D)coord; @end
Then open the corresponding SimpleAnnotation.m implementation file and make the changes shown:
#import "SimpleAnnotation.h" @implementation SimpleAnnotation @synthesize coordinate=_coordinate; @synthesize title=_title; @synthesize subtitle=_subtitle; + (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord { return [[[self class] alloc] initWithCoordinate:coord]; } - (id)initWithCoordinate:(CLLocationCoordinate2D)coord { if ( self = [super init] ) { self.coordinate = coord; } return self; } @end
The SimpleAnnotation class is just a container; it implements the MKAnnotation protocol to allow it to hold the coordinates and title (with subtitle) of our annotation.
Click the ViewController.h interface file to open in the Xcode editor, and import the SimpleAnnotation header file:
#import "SimpleAnnotation.h"
Then, in the viewDidLoad: method, add two annotations to our mapView as follows:
CLLocationCoordinate2D moffett = {37.4163, −122.0519}; SimpleAnnotation *moffettAnnotation = [[SimpleAnnotation alloc] initWithCoordinate:moffett]; moffettAnnotation.title = @"Moffett Federal Airfield"; moffettAnnotation.subtitle = @"37.4163, −122.0519"; [self.mapView addAnnotation: moffettAnnotation]; CLLocationCoordinate2D sanJose = {37.3647, −121.9338}; SimpleAnnotation *sanJoseAnnotation = [[SimpleAnnotation alloc] initWithCoordinate:sanJose]; sanJoseAnnotation.title = @"San Jose International"; sanJoseAnnotation.subtitle = @"37.3647, −121.9338"; [self.mapView addAnnotation: sanJoseAnnotation];
Save your changes, and change to the AppDelagate.m. You’ll have to expand your view a little bit as your map window currently excludes your new markers. Go into the animateMap: method and change the size of your view from 2 miles to 20 miles.
double miles = 20.0;
Then click the Run button in the Xcode toolbar to compile and deploy your application in the iPhone Simulator. If all goes well, you should see something much like Figure 3-6.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27094)
Hello! Python by Anthony Briggs(25950)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25286)
Kotlin in Action by Dmitry Jemerov(24394)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23313)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21945)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20848)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17073)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16833)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12584)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10650)
Hit Refresh by Satya Nadella(9238)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8446)
Robo-Advisor with Python by Aki Ranin(8387)